﻿using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
using System.Collections.ObjectModel;
using System.Diagnostics;
using LiveChartsCore.Drawing;
using LiveChartsCore.Kernel.Sketches;
using LiveChartsCore.SkiaSharpView.Painting.Effects;
using NPOI.SS.Formula.Functions;
using 基于WPF的电缆采样数据分析系统.Tools.MessagePopupTool;
using 基于WPF的电缆采样数据分析系统.Tools.LangugeTool;
using 基于WPF的电缆采样数据分析系统.Models.RefWav;
using 基于WPF的电缆采样数据分析系统.Models.Enums;
using System.Windows;
using System.Windows.Documents;
using System.Collections.Specialized;
using 基于WPF的电缆采样数据分析系统.MsgEvents.AccountingReport;

namespace 基于WPF的电缆采样数据分析系统.ViewModels.TDRWork
{
    public class RealTimeOscilloscopeViewModel :BindableBase
    {


        #region 单例模型

        // 创建单例
        public static RealTimeOscilloscopeViewModel Instance = new RealTimeOscilloscopeViewModel();

        #endregion


        /// <summary>
        /// 随机数
        /// </summary>
        private readonly Random _random = new();

        /// <summary>
        /// 波形是否刷新
        /// </summary>
        public bool IsReading { get; set; } = true;

        /// <summary>
        /// 同步锁
        /// </summary>
        public object Sync { get; } = new object();


        /// <summary>
        /// 当前波形
        /// </summary>
        List<ObservablePoint> currentWavList { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 仿真波形
        /// </summary>
        List<ObservablePoint> SimulationWavList { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 是否导入波形
        /// </summary>
        public bool IsImportWav { get; set; } = false;


        /// <summary>
        /// 构造器
        /// </summary>
        public RealTimeOscilloscopeViewModel()
        {
            //Fetch();
            // 初始化波形
            Series = [
            new LineSeries<ObservablePoint>
            {
                Values = currentWavList,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR1,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR2,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR3,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR4,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR5,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR6,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR7,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR8,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
            ];

            // 异步线程
            //_ = InitWavform();
        }

        #region 示波器样式
        /// <summary>
        /// 样式颜色
        /// </summary>
        private static  SKColor s_gray = new(195, 195, 195);
        private static  SKColor s_gray1 = new(160, 160, 160);
        private static  SKColor s_gray2 = new(90, 90, 90);
        private static  SKColor s_dark3 = new(60, 60, 60);
        /// <summary>
        /// 波形集合
        /// </summary>
        private ObservableCollection<ISeries> _Series;

        /// <summary>
        /// 波形集合
        /// </summary>
        public ObservableCollection<ISeries> Series
        {
            get { return _Series; }
            set { 
                _Series = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 替换方法
        /// </summary>
        /// <param name="index"></param>
        /// <param name="newItem"></param>
        public void ReplaceItem(int index, ISeries newItem)
        {
            Series[index] = newItem; // 直接替换项

            // 手动触发 CollectionChanged 事件
            RaisePropertyChanged("Series");
        }


        /// <summary>
        /// X轴样式
        /// </summary>
        public ICartesianAxis[] XAxes { get; set; } = [
            new Axis
        {
            Name = "ns",
            NamePaint = new SolidColorPaint(new(160, 160, 160)),
            TextSize = 18,
            Padding = new Padding(5, 15, 5, 5),
            LabelsPaint = new SolidColorPaint(new(195, 195, 195)),
            SeparatorsPaint = new SolidColorPaint
            {
                Color = new(195, 195, 195),
                StrokeThickness = 1,
                PathEffect = new DashEffect([3, 3])
            },
            SubseparatorsPaint = new SolidColorPaint
            {
                Color = new(90, 90, 90),
                StrokeThickness = 0.5f
            },
            SubseparatorsCount = 9,
            ZeroPaint = new SolidColorPaint
            {
                Color = new(160, 160, 160),
                StrokeThickness = 2
            },
            TicksPaint = new SolidColorPaint
            {
                Color = new(195, 195, 195),
                StrokeThickness = 1.5f
            },
            SubticksPaint = new SolidColorPaint
            {
                Color = new(195, 195, 195),
                StrokeThickness = 1
            }
        }
        ];

        /// <summary>
        /// Y轴样式
        /// </summary>
        public ICartesianAxis[] YAxes { get; set; } = [
            new Axis
        {
            Name = "mV",
            NamePaint = new SolidColorPaint(new(160, 160, 160)),
            TextSize = 18,
            Padding = new Padding(5, 0, 15, 0),
            LabelsPaint = new SolidColorPaint(new(195, 195, 195)),
            SeparatorsPaint = new SolidColorPaint
            {
                Color = new(195, 195, 195),
                StrokeThickness = 1,
                PathEffect = new DashEffect([3, 3])
            },
            SubseparatorsPaint = new SolidColorPaint
            {
                Color = new(90, 90, 90),
                StrokeThickness = 0.5f
            },
            SubseparatorsCount = 9,
            ZeroPaint = new SolidColorPaint
            {
                Color = new(160, 160, 160),
                StrokeThickness = 2
            },
            TicksPaint = new SolidColorPaint
            {
                Color = new(195, 195, 195),
                StrokeThickness = 1.5f
            },
            SubticksPaint = new SolidColorPaint
            {
                Color = new(195, 195, 195),
                StrokeThickness = 1
            }
        }
        ];

        /// <summary>
        /// 背景和边框
        /// </summary>
        public DrawMarginFrame Frame { get; set; } =
            new()
            {
                Fill = new SolidColorPaint(new(60, 60, 60)),
                Stroke = new SolidColorPaint
                {
                    Color = new(195, 195, 195),
                    StrokeThickness = 1
                }
            };

        #endregion

      
        #region 添加波形
        /// <summary>
        /// 添加波形文件
        /// </summary>
        /// <param name="wavList"></param>
        public async Task ShowWavform(UpdateWavList updateWavList, bool IsResetSamplingCount)
        {

            if (updateWavList == null)
            {
                return;
            }


            // 采样次数重置
            IsShow = Visibility.Visible;

            // 是否重置采样次数
            if (IsResetSamplingCount)
            {
                SamplingCount = 0;
            }

            // 允许缩放
            IsImportWav = true;
            // 允许刷新
            IsReading = true;

            if (currentWavList != null)
            {
                currentWavList.Clear();
            }

            var list = new List<ObservablePoint>();

            if (updateWavList.WavList == null)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("NoWaveformDataIsInjectedIntoTheCurrentChannel"));
                return;
            }

            try
            {

                // Y轴偏移
                for (int i = 0; i < updateWavList.WavList.Count; i++)
                {
                    updateWavList.WavList[i] += updateWavList.YAxisTranslation;
                }

                // 波形集合渲染
                for (int i = 0; i < updateWavList.WavList.Count; i++)
                {
                    list.Add(new()
                    {
                        X = i + (updateWavList.XAxisTranslation),
                        Y = updateWavList.WavList[i]
                    });
                }


            }
            catch (Exception ex)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("CauseOfError") + ex.Message);
            }


            lock (Sync)
            {

                currentWavList = list;
                SimulationWavList = list;

                // 替换波形
                ReplaceItem(0, new LineSeries<ObservablePoint>
                {
                    Values = currentWavList,
                    Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                    Fill = null,
                    GeometrySize = 0
                });

            }


            ToggleContinuationWav(IsReading);
        }
        #endregion 


        #region 清除波形

        /// <summary>
        /// 清除波形
        /// </summary>
        public void ClearWavform()
        {
            IsReading = false;
            
            currentWavList = new List<ObservablePoint>();
            Series = [
            new LineSeries<ObservablePoint>
            {
                Values = currentWavList,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR1,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR2,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR3,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR4,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR5,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR6,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR7,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
             new LineSeries<ObservablePoint>
            {
                Values = currentWavListR8,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            },
            ];

            // 不允许缩放
            if (!(GlobalVariableEdit.R1Display && GlobalVariableEdit.R1WavList != null ||
                GlobalVariableEdit.R2Display && GlobalVariableEdit.R2WavList != null ||
                GlobalVariableEdit.R3Display && GlobalVariableEdit.R3WavList != null ||
                GlobalVariableEdit.R4Display && GlobalVariableEdit.R4WavList != null ||
                GlobalVariableEdit.R5Display && GlobalVariableEdit.R5WavList != null ||
                GlobalVariableEdit.R6Display && GlobalVariableEdit.R6WavList != null ||
                GlobalVariableEdit.R7Display && GlobalVariableEdit.R7WavList != null ||
                GlobalVariableEdit.R8Display && GlobalVariableEdit.R8WavList != null ))
            {
                IsImportWav = false;
            }
            
            // 采样次数重置
            SamplingCount = 0;
            IsShow = Visibility.Collapsed;
        }

        #endregion


        #region 仿真波形

        /// <summary>
        /// 启动波形显示
        /// </summary>
        public async void ToggleContinuationWav(bool isRead)
        {
            while (isRead)
            {
                await Task.Delay(500);

                lock (Sync)
                {
                    SimulatedWaveform(SimulationWavList);
                }
            }
        }

        /// <summary>
        /// 仿真波形
        /// </summary>
        /// <param name="wavList"></param>
        private void SimulatedWaveform(List<ObservablePoint> simulationWavList)
        {

            if (!IsReading)
            {
                return;
            }

            List<ObservablePoint> WavList = new List<ObservablePoint>();


            for (int i = 0; i < simulationWavList.Count; i++)
            {
                WavList.Add(new ObservablePoint()
                {
                    X = simulationWavList[i].X,
                    Y = GenerateSignSymbol() ? simulationWavList[i].Y + _random.Next(0, 2) : simulationWavList[i].Y - _random.Next(0, 2),
                });
            }

            // 替换波形
            ReplaceItem(0, new LineSeries<ObservablePoint>
            {
                Values = WavList,
                Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                Fill = null,
                GeometrySize = 0
            });

            // 采样次数累加
            if ((DateTime.Now - BeforeRefreshTime) >= TimeSpan.FromMilliseconds(500))
            {
                SamplingCount += 1;
                BeforeRefreshTime = DateTime.Now;
            }
            
        }

        /// <summary>
        /// 随机返回正负符号
        /// </summary>
        /// <returns></returns>
        private bool GenerateSignSymbol()
        {
            if (_random.Next(1, 100) % 2 == 0)
            {
                return true;
            }

            return false;
        }

        private void DeleteFirstElement()
        {
            if (Series.Count >= 2)
            {
                Series.RemoveAt(0);
            }
        }

        #endregion


        #region 参考波形
        /// <summary>
        /// 当前波形R1
        /// </summary>
        List<ObservablePoint> currentWavListR1 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 仿真波形R1
        /// </summary>
        List<ObservablePoint> SimulationWavListR1 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 当前波形R2
        /// </summary>
        List<ObservablePoint> currentWavListR2 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 仿真波形R2
        /// </summary>
        List<ObservablePoint> SimulationWavListR2 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 当前波形R3
        /// </summary>
        List<ObservablePoint> currentWavListR3 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 仿真波形R3
        /// </summary>
        List<ObservablePoint> SimulationWavListR3 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 当前波形R4
        /// </summary>
        List<ObservablePoint> currentWavListR4 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 仿真波形R4
        /// </summary>
        List<ObservablePoint> SimulationWavListR4 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 当前波形R5
        /// </summary>
        List<ObservablePoint> currentWavListR5 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 仿真波形R5
        /// </summary>
        List<ObservablePoint> SimulationWavListR5 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 当前波形R6
        /// </summary>
        List<ObservablePoint> currentWavListR6 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 仿真波形R6
        /// </summary>
        List<ObservablePoint> SimulationWavListR6 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 当前波形R7
        /// </summary>
        List<ObservablePoint> currentWavListR7 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 仿真波形R7
        /// </summary>
        List<ObservablePoint> SimulationWavListR7 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 当前波形R8
        /// </summary>
        List<ObservablePoint> currentWavListR8 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 仿真波形R8
        /// </summary>
        List<ObservablePoint> SimulationWavListR8 { get; set; } = new List<ObservablePoint>();

        /// <summary>
        /// 同步锁R1
        /// </summary>
        public object SyncR1 { get; } = new object();

        /// <summary>
        /// 同步锁R2
        /// </summary>
        public object SyncR2 { get; } = new object();

        /// <summary>
        /// 同步锁R3
        /// </summary>
        public object SyncR3 { get; } = new object();

        /// <summary>
        /// 同步锁R4
        /// </summary>
        public object SyncR4 { get; } = new object();

        /// <summary>
        /// 同步锁R5
        /// </summary>
        public object SyncR5 { get; } = new object();

        /// <summary>
        /// 同步锁R6
        /// </summary>
        public object SyncR6 { get; } = new object();

        /// <summary>
        /// 同步锁R7
        /// </summary>
        public object SyncR7 { get; } = new object();

        /// <summary>
        /// 同步锁R8
        /// </summary>
        public object SyncR8 { get; } = new object();

        /// <summary>
        /// R1波形是否显示
        /// </summary>
        public bool R1Display { get; set; }

        /// <summary>
        /// R2波形是否显示
        /// </summary>
        public bool R2Display { get; set; }

        /// <summary>
        /// R3波形是否显示
        /// </summary>
        public bool R3Display { get; set; }

        /// <summary>
        /// R4波形是否显示
        /// </summary>
        public bool R4Display { get; set; }

        /// <summary>
        /// R5波形是否显示
        /// </summary>
        public bool R5Display { get; set; }

        /// <summary>
        /// R6波形是否显示
        /// </summary>
        public bool R6Display { get; set; }

        /// <summary>
        /// R7波形是否显示
        /// </summary>
        public bool R7Display { get; set; }

        /// <summary>
        /// R8波形是否显示
        /// </summary>
        public bool R8Display { get; set; }
        #endregion


        #region 保存参考波形
        /// <summary>
        /// 根据类型显示参考波形
        /// </summary>
        public async Task ByTypeDisplayRefWavform(RefWavformType R)
        {
            switch (R)
            {
                case RefWavformType.RefWavformOne:
                    await DisplayRefWavform(new SolidColorPaint(new SKColor(255, 0, 0), 1f), RefreshWav(GlobalVariableEdit.R1WavList), 1, GlobalVariableEdit.R1Display);
                    R1Display = GlobalVariableEdit.R1Display;
                    break;
                case RefWavformType.RefWavformTwo:
                    await DisplayRefWavform(new SolidColorPaint(new SKColor(255, 165, 0), 1f), RefreshWav(GlobalVariableEdit.R2WavList), 2, GlobalVariableEdit.R2Display);
                    R2Display = GlobalVariableEdit.R2Display;
                    break;
                case RefWavformType.RefWavformThree:
                    await DisplayRefWavform(new SolidColorPaint(new SKColor(255, 255, 0), 1f), RefreshWav(GlobalVariableEdit.R3WavList), 3, GlobalVariableEdit.R3Display);
                    R3Display = GlobalVariableEdit.R3Display;
                    break;
                case RefWavformType.RefWavformFour:
                    await DisplayRefWavform(new SolidColorPaint(new SKColor(0, 128, 0), 1f), RefreshWav(GlobalVariableEdit.R4WavList), 4, GlobalVariableEdit.R4Display);
                    R4Display = GlobalVariableEdit.R4Display;
                    break;
                case RefWavformType.RefWavformFive:
                    await DisplayRefWavform(new SolidColorPaint(new SKColor(0, 255, 255), 1f), RefreshWav(GlobalVariableEdit.R5WavList), 5, GlobalVariableEdit.R5Display);
                    R5Display = GlobalVariableEdit.R5Display;
                    break;
                case RefWavformType.RefWavformSix:
                    await DisplayRefWavform(new SolidColorPaint(new SKColor(128, 0, 128), 1f), RefreshWav(GlobalVariableEdit.R6WavList), 6, GlobalVariableEdit.R6Display);
                    R6Display = GlobalVariableEdit.R6Display;
                    break;
                case RefWavformType.RefWavformSeven:
                    await DisplayRefWavform(new SolidColorPaint(new SKColor(255, 255, 255), 1f), RefreshWav(GlobalVariableEdit.R7WavList), 7, GlobalVariableEdit.R7Display);
                    R7Display = GlobalVariableEdit.R7Display;
                    break;
                case RefWavformType.RefWavformEight:
                    await DisplayRefWavform(new SolidColorPaint(new SKColor(135, 206, 235), 1f), RefreshWav(GlobalVariableEdit.R8WavList), 8, GlobalVariableEdit.R8Display);
                    R8Display = GlobalVariableEdit.R8Display;
                    break;
            }
        }

        private UpdateWavList RefreshWav(UpdateWavList updateWavList)
        {
            if (updateWavList == null)
            {
                return null;
            }

            UpdateWavList newUpdateWavList = new UpdateWavList();
            List<float> newWavList = new List<float>();
            for (int i = 0; i < updateWavList.WavList.Count; i++)
            {
                newWavList.Add(updateWavList.WavList[i]);
            }

            newUpdateWavList.WavList = newWavList;
            newUpdateWavList.XAxisTranslation = updateWavList.XAxisTranslation;
            newUpdateWavList.YAxisTranslation = updateWavList.YAxisTranslation;

            return newUpdateWavList;
        }

        /// <summary>
        /// 显示对应的参考波形
        /// </summary>
        /// <param name="colorAndSolid"></param>
        /// <param name="updateWavList"></param>
        /// <returns></returns>
        public async Task DisplayRefWavform(SolidColorPaint colorAndSolid, UpdateWavList updateWavList, int IndexNumber, bool isRead)
        {
            if (updateWavList == null)
            {
                return;
            }

            if (updateWavList.WavList == null)
            {
                return;
            }

            // 允许缩放
            IsImportWav = true;

            var list = new List<ObservablePoint>();

            try
            {
                var newList = new List<float>();

                // Y轴偏移
                for (int i = 0; i < updateWavList.WavList.Count; i++)
                {
                    newList.Add(updateWavList.WavList[i]);
                }

                updateWavList.WavList = newList;

                // 波形集合渲染
                for (int i = 0; i < updateWavList.WavList.Count; i++)
                {
                    list.Add(new()
                    {
                        X = i + (updateWavList.XAxisTranslation),
                        Y = newList[i]
                    });
                }


            }
            catch (Exception ex)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("CauseOfError") + ex.Message);
            }


            switch (IndexNumber)
            {
                case 1:
                    SimulationWavListR1 = list;
                    ToggleContinuationRefWavR1(IndexNumber, colorAndSolid);
                    break;
                case 2:
                    SimulationWavListR2 = list;
                    ToggleContinuationRefWavR2(IndexNumber, colorAndSolid);
                    break;
                case 3:
                    SimulationWavListR3 = list;
                    ToggleContinuationRefWavR3(IndexNumber, colorAndSolid);
                    break;
                case 4:
                    SimulationWavListR4 = list;
                    ToggleContinuationRefWavR4(IndexNumber, colorAndSolid);
                    break;
                case 5:
                    SimulationWavListR5 = list;
                    ToggleContinuationRefWavR5(IndexNumber, colorAndSolid);
                    break;
                case 6:
                    SimulationWavListR6 = list;
                    ToggleContinuationRefWavR6(IndexNumber, colorAndSolid);
                    break;
                case 7:
                    SimulationWavListR7 = list;
                    ToggleContinuationRefWavR7(IndexNumber, colorAndSolid);
                    break;
                case 8:
                    SimulationWavListR8 = list;
                    ToggleContinuationRefWavR8(IndexNumber, colorAndSolid);
                    break;
            }

        }


        /// <summary>
        /// 启动波形显示R1
        /// </summary>
        public async void ToggleContinuationRefWavR1(int IndexNumber, SolidColorPaint colorAndSolid)
        {
            while (R1Display)
            {
                await Task.Delay(500);

                lock (SyncR1)
                {
                    SimulatedRefWaveform(SimulationWavListR1, IndexNumber, GlobalVariableEdit.R1Display, colorAndSolid);
                }
            }
        }

        /// <summary>
        /// 启动波形显示R2
        /// </summary>
        public async void ToggleContinuationRefWavR2(int IndexNumber, SolidColorPaint colorAndSolid)
        {
            while (R2Display)
            {
                await Task.Delay(500);

                lock (SyncR2)
                {
                    SimulatedRefWaveform(SimulationWavListR2, IndexNumber, GlobalVariableEdit.R2Display, colorAndSolid);
                }
            }
        }

        /// <summary>
        /// 启动波形显示R3
        /// </summary>
        public async void ToggleContinuationRefWavR3(int IndexNumber, SolidColorPaint colorAndSolid)
        {
            while (R3Display)
            {
                await Task.Delay(500);

                lock (SyncR3)
                {
                    SimulatedRefWaveform(SimulationWavListR3, IndexNumber, GlobalVariableEdit.R3Display, colorAndSolid);
                }
            }
        }

        /// <summary>
        /// 启动波形显示R4
        /// </summary>
        public async void ToggleContinuationRefWavR4(int IndexNumber, SolidColorPaint colorAndSolid)
        {
            while (R4Display)
            {
                await Task.Delay(500);

                lock (SyncR4)
                {
                    SimulatedRefWaveform(SimulationWavListR4, IndexNumber, GlobalVariableEdit.R4Display, colorAndSolid);
                }
            }
        }


        /// <summary>
        /// 启动波形显示R5
        /// </summary>
        public async void ToggleContinuationRefWavR5(int IndexNumber, SolidColorPaint colorAndSolid)
        {
            while (R5Display)
            {
                await Task.Delay(500);

                lock (SyncR5)
                {
                    SimulatedRefWaveform(SimulationWavListR5, IndexNumber, GlobalVariableEdit.R5Display, colorAndSolid);
                }
            }
        }

        /// <summary>
        /// 启动波形显示R6
        /// </summary>
        public async void ToggleContinuationRefWavR6(int IndexNumber, SolidColorPaint colorAndSolid)
        {
            while (R6Display)
            {
                await Task.Delay(500);

                lock (SyncR6)
                {
                    SimulatedRefWaveform(SimulationWavListR6, IndexNumber, GlobalVariableEdit.R6Display, colorAndSolid);
                }
            }
        }

        /// <summary>
        /// 启动波形显示R7
        /// </summary>
        public async void ToggleContinuationRefWavR7(int IndexNumber, SolidColorPaint colorAndSolid)
        {
            while (R7Display)
            {
                await Task.Delay(500);

                lock (SyncR7)
                {
                    SimulatedRefWaveform(SimulationWavListR7, IndexNumber, GlobalVariableEdit.R7Display, colorAndSolid);
                }
            }
        }

        /// <summary>
        /// 启动波形显示R8
        /// </summary>
        public async void ToggleContinuationRefWavR8(int IndexNumber, SolidColorPaint colorAndSolid)
        {
            while (R8Display)
            {
                await Task.Delay(500);

                lock (SyncR8)
                {
                    SimulatedRefWaveform(SimulationWavListR8, IndexNumber, GlobalVariableEdit.R8Display, colorAndSolid);
                }
            }
        }


        /// <summary>
        /// 仿真波形
        /// </summary>
        /// <param name="wavList"></param>
        private void SimulatedRefWaveform(List<ObservablePoint> simulationWavList, int IndexNumber, bool isRead, SolidColorPaint colorAndSolid)
        {

            if (!isRead)
            {
                return;
            }

            List<ObservablePoint> WavList = new List<ObservablePoint>();


            for (int i = 0; i < simulationWavList.Count; i++)
            {
                WavList.Add(new ObservablePoint()
                {
                    X = simulationWavList[i].X,
                    Y = GenerateSignSymbol() ? simulationWavList[i].Y + _random.Next(0, 2) : simulationWavList[i].Y - _random.Next(0, 2),
                });
            }


            // 替换波形
            ReplaceItem(IndexNumber, new LineSeries<ObservablePoint>
            {
                Values = WavList,
                Stroke = colorAndSolid,
                Fill = null,
                GeometrySize = 0
            });


        }

        #endregion


        #region 采样次数
        /// <summary>
        /// 采样次数
        /// </summary>
        private int _SamplingCount = 0;

        /// <summary>
        /// 采样次数
        /// </summary>
        public int SamplingCount
        {
            get { return _SamplingCount; }
            set { 
                _SamplingCount = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 采样次数是否显示
        /// </summary>
        private Visibility _IsShow = Visibility.Collapsed;

        /// <summary>
        /// 采样次数是否显示
        /// </summary>
        public Visibility IsShow
        {
            get { return _IsShow; }
            set { 
                _IsShow = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 上一次刷新时间
        /// </summary>
        public DateTime BeforeRefreshTime { get; set; } = DateTime.Now;
        #endregion


        #region 隐藏参考波形
        /// <summary>
        /// 隐藏参考波形
        /// </summary>
        public void HiddenRefWavform(RefWavformType R)
        {
            switch (R)
            {
                case RefWavformType.RefWavformOne:
                    GlobalVariableEdit.R1Display = false;
                    // 清除波形
                    ReplaceItem(1, new LineSeries<ObservablePoint>
                    {
                        Values = null,
                        Stroke = new SolidColorPaint(new SKColor(255, 255, 255), 1f),
                        Fill = null,
                        GeometrySize = 0
                    });
                    break;
                case RefWavformType.RefWavformTwo:
                    GlobalVariableEdit.R2Display = false;
                    // 清除波形
                    ReplaceItem(2, new LineSeries<ObservablePoint>
                    {
                        Values = null,
                        Stroke = new SolidColorPaint(new SKColor(255, 255, 255), 1f),
                        Fill = null,
                        GeometrySize = 0
                    });
                    break;
                case RefWavformType.RefWavformThree:
                    GlobalVariableEdit.R3Display = false;
                    // 清除波形
                    ReplaceItem(3, new LineSeries<ObservablePoint>
                    {
                        Values = null,
                        Stroke = new SolidColorPaint(new SKColor(255, 255, 255), 1f),
                        Fill = null,
                        GeometrySize = 0
                    });
                    break;
                case RefWavformType.RefWavformFour:
                    GlobalVariableEdit.R4Display = false;
                    // 清除波形
                    ReplaceItem(4, new LineSeries<ObservablePoint>
                    {
                        Values = null,
                        Stroke = new SolidColorPaint(new SKColor(255, 255, 255), 1f),
                        Fill = null,
                        GeometrySize = 0
                    });
                    break;
                case RefWavformType.RefWavformFive:
                    GlobalVariableEdit.R5Display = false;
                    // 清除波形
                    ReplaceItem(5, new LineSeries<ObservablePoint>
                    {
                        Values = null,
                        Stroke = new SolidColorPaint(new SKColor(255, 255, 255), 1f),
                        Fill = null,
                        GeometrySize = 0
                    });
                    break;
                case RefWavformType.RefWavformSix:
                    GlobalVariableEdit.R6Display = false;
                    // 清除波形
                    ReplaceItem(6, new LineSeries<ObservablePoint>
                    {
                        Values = null,
                        Stroke = new SolidColorPaint(new SKColor(255, 255, 255), 1f),
                        Fill = null,
                        GeometrySize = 0
                    });
                    break;
                case RefWavformType.RefWavformSeven:
                    GlobalVariableEdit.R7Display = false;
                    // 清除波形
                    ReplaceItem(7, new LineSeries<ObservablePoint>
                    {
                        Values = null,
                        Stroke = new SolidColorPaint(new SKColor(255, 255, 255), 1f),
                        Fill = null,
                        GeometrySize = 0
                    });
                    break;
                case RefWavformType.RefWavformEight:
                    GlobalVariableEdit.R8Display = false;
                    // 清除波形
                    ReplaceItem(8, new LineSeries<ObservablePoint>
                    {
                        Values = null,
                        Stroke = new SolidColorPaint(new SKColor(255, 255, 255), 1f),
                        Fill = null,
                        GeometrySize = 0
                    });
                    break;
            }

            
        }

        #endregion


        #region 清除参考波形
        /// <summary>
        /// 清除参考波形
        /// </summary>
        private void ClearRefWavform()
        {

        }

        #endregion


        #region 单次测试
        /// <summary>
        /// 添加波形文件
        /// </summary>
        /// <param name="wavList"></param>
        public async Task SingleTestWavform(UpdateWavList updateWavList)
        {

            if (updateWavList == null)
            {
                return;
            }

            // 采样次数重置
            IsShow = Visibility.Collapsed;

            // 是否重置采样次数
            if (true)
            {
                SamplingCount = 0;
            }

            // 允许缩放
            IsImportWav = true;
            // 允许波动
            IsReading = false;

            if (currentWavList != null)
            {
                currentWavList.Clear();
            }

            var list = new List<ObservablePoint>();

            if (updateWavList.WavList == null)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("NoWaveformIsDisplayed"));
                return;
            }

            try
            {
                var newCurrentList = new List<float>(updateWavList.WavList.Count);

                // Y轴偏移
                for (int i = 0; i < updateWavList.WavList.Count; i++)
                {
                    newCurrentList.Add(updateWavList.WavList[i]);
                }

                updateWavList.WavList = newCurrentList;

                // 波形集合渲染
                for (int i = 0; i < updateWavList.WavList.Count; i++)
                {
                    list.Add(new()
                    {
                        X = i + (updateWavList.XAxisTranslation),
                        Y = updateWavList.WavList[i]
                    });
                }

                
            }
            catch (Exception ex)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("CauseOfError") + ex.Message);
            }


            lock (Sync)
            {

                currentWavList = list;

                // 替换波形
                ReplaceItem(0, new LineSeries<ObservablePoint>
                {
                    Values = currentWavList,
                    Stroke = new SolidColorPaint(new SKColor(33, 150, 243), 1f),
                    Fill = null,
                    GeometrySize = 0
                });
            }

        }

        #endregion
    }
}
